home *** CD-ROM | disk | FTP | other *** search
- /* $Id: $ */
-
- /*
- * Minimal 'stat' emulation: tells directories from files and
- * gives length and mtime.
- *
- * Largely based on code written by Guido van Rossum, CWI, Amsterdam
- * and placed by him in the public domain --
- * retrieved by anonymous FTP from ftp.cwi.nl
- */
- /*
- * Original from ThinkCPosix by Timothy Murphy <tim@maths.tcd.ie>,
- * Trinity College Dublin
- *
- * Modified by SPDsoft <macspd@ivo.cps.unizar.es> to use Unix dates
- * seconds from Mac 'Fri Jan 1 00:00:00 1904' to
- * Un*x 'Thu Jan 1 00:00:00 1970': 2082844800
- */
-
- #define U2MSEC 2082844800
-
-
- #include <sys/stat.h>
- #include <errno.h>
- #include <stdio.h>
- #include <fcntl.h>
-
- /*
- * Stupid CodeWarrior 1.0a1s does not defines these
- */
-
- #ifndef EACCES
- #define EACCES (-54)
- #endif
- #ifndef ENOENT
- #define ENOENT (-43)
- #endif
-
- FILE *fdopen(int, char *); /* I don't want to include unix.h */
-
-
- int sys_nerr = 0;
- char *sys_errlist[] = {""};
- char *myenviron[] = {NULL};
- char **environ = myenviron;
- extern int __uid, __gid;
- int Stat(char*, long, struct stat*);
-
-
- /* Bits in ioFlAttrib: */
- #define LOCKBIT (1<<0) /* File locked */
- #define DIRBIT (1<<4) /* It's a directory */
-
- /*
- * Mac-ky "stat" in which filename is given relative to a directory,
- * specified by long DirID.
- */
-
- int
- Stat(name, DirID, buf)
- char *name;
- long DirID;
- struct stat *buf;
- {
- CInfoPBRec cipbr;
- HFileInfo *fpb = (HFileInfo*)&cipbr;
- DirInfo *dpb = (DirInfo*)&cipbr;
- unsigned char pname[256];
- short err;
-
- strcpy((char*)pname, name);
- c2pstr((char*)pname);
-
- dpb->ioDrDirID = DirID;
- fpb->ioNamePtr = pname;
- fpb->ioVRefNum = 0;
- fpb->ioFDirIndex = 0;
- fpb->ioFVersNum = 0;
- err = PBGetCatInfo(&cipbr, FALSE);
- if (err != noErr) {
- errno = ENOENT;
- return -1;
- }
- if (fpb->ioFlAttrib & LOCKBIT)
- buf->st_mode= 0444;
- else
- buf->st_mode= 0666;
- if (fpb->ioFlAttrib & DIRBIT) {
- buf->st_mode |= 0111 | S_IFDIR;
- buf->st_size= dpb->ioDrNmFls;
- buf->st_rsize= 0;
- }
- else {
- buf->st_mode |= S_IFREG;
- if (fpb->ioFlFndrInfo.fdType == 'APPL')
- buf->st_mode |= 0111;
- buf->st_size= fpb->ioFlLgLen;
- buf->st_rsize= fpb->ioFlRLgLen;
- }
- buf->st_atime= fpb->ioFlCrDat - U2MSEC;
- buf->st_mtime= fpb->ioFlMdDat - U2MSEC;
- buf->st_ctime= fpb->ioFlCrDat - U2MSEC;
- buf->st_ino= (unsigned short)fpb->ioDirID;
- buf->st_uid= __uid;
- buf->st_gid= __gid;
- buf->st_FlFndrInfo= fpb->ioFlFndrInfo;
- return 0;
- }
-
- int
- stat(path, buf)
- char *path;
- struct stat *buf;
- {
- return Stat(path, 0L, buf);
- }
-
- int
- fstat(fd, buf)
- int fd;
- struct stat *buf;
- {
- #if defined(THINK_C)
- FCBPBRec fcb;
- unsigned char pname[256];
- long DirID;
- short err;
-
- /*
- * fdopen() gives FILE entry with name of file,
- * as well as RefNum of containing directory
- */
-
- FILE *fp = fdopen(fd, "");
-
- /*
- * PBGetFCBInfo() converts short RefNum to long DirID
- */
-
- fcb.ioRefNum= fp->refnum;
- fcb.ioVRefNum= 0;
- fcb.ioFCBIndx= 0;
- fcb.ioNamePtr= pname;
- err= PBGetFCBInfo(&fcb, FALSE);
- if (err != noErr) {
- errno = ENOENT;
- return -1;
- }
- DirID = fcb.ioFCBParID;
-
- p2cstr(pname);
- return Stat((char*)pname, DirID, buf);
-
- #else
- # if !defined(__MWERKS__)
- int err;
- char fname[256];
-
- err = ioctl( fd, FIOFNAME, (long *)fname );
- if( err == 0 ) return Stat( fname, 0L, statPtr );
- else err = -1;
-
- return err;
- # else
- return -1;
- # endif
- #endif
- }
-
-